<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.provinceCode"
        placeholder="省份代码"
        style="width: 100px;"
      />
      <el-input
        v-model="listQuery.cityCode"
        placeholder="城市代码"
        style="width: 0px;"
      />
      <el-input
        v-model="listQuery.cityName"
        placeholder="城市名称"
        style="width: 0px;"
      />
      <el-button type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button
        v-if="isP('DicCityAdd')"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleAdd"
      >
        新增
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      stripe
      highlight-current-row
    >
      <el-table-column
        type="index"
        width="50"
      />
      <el-table-column
        prop="provinceCode"
        label="省份代码"
        width="100"
      />
      <el-table-column
        prop="cityCode"
        label="城市代码"
        width="0"
      />
      <el-table-column
        prop="cityName"
        label="城市名称"
        width="0"
      />
      <el-table-column align="center" label="操作" width="240">
        <template slot-scope="scope">
          <el-button
            v-if="isP('DicCityGet')"
            style="margin-left: 10px"
            type="success"
            size="mini"
            @click="handleInfo(scope.row)"
          >详细
          </el-button>
          <el-button
            v-if="isP('DicCityUpdate')"
            style="margin-left: 10px"
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-if="isP('DicCityDelete')"
            style="margin-left: 10px"
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="listDicCity"
    />
  </div>
</template>

<script>
import { listDicCity, deleteDicCity } from '@/api/dic/city'
import Pagination from '@/components/Pagination/index'
import msgUtil from '@/utils/msgUtil'

export default {
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        provinceCode: null,
        cityCode: null,
        cityName: null,
        page: 1,
        limit: 20
      }
    }
  },
  created() {
    this.listDicCity()
  },
  methods: {
    listDicCity() {
      this.listLoading = true
      listDicCity(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      })
    },
    // 查询
    handleFilter() {
      this.listQuery.page = 1
      this.listDicCity()
    },
    // 删除
    handleDelete(row) {
      msgUtil.confirm('确认删除该城市字典?').then(async() => {
        await deleteDicCity({ cityCode: row.cityCode })
        msgUtil.success2('删除成功!')
        this.listDicCity()
      })
    },
    // 修改
    handleUpdate(row) {
      this.$router.push({
        path: '/dic/city/update',
        query: { cityCode: row.cityCode }
      })
    },
    // 查看详情
    handleInfo(row) {
      this.$router.push({
        path: '/dic/city/info',
        query: { cityCode: row.cityCode }
      })
    },
    // 新增
    handleAdd() {
      this.$router.push({
        path: '/dic/city/add',
        query: {}
      })
    }
  }
}
</script>
